package village.rainbow.dailyscene.uicontrol;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class UiPagingScrollView extends HorizontalScrollView {

	private boolean mIsRunning;

	private LinearLayout mLayout;
	private PageScrollCompletedListener mPageScrollCompletedListener;

	public UiPagingScrollView(Context context, int width, int height) {
		this(context, null);
		this.setLayoutParams(new LayoutParams(width, height));
		super.setScrollbarFadingEnabled(true);
		super.setSmoothScrollingEnabled(true);
		super.setHorizontalScrollBarEnabled(false);
	}
	
	public UiPagingScrollView(Context context, boolean isBot) {
		this(context, null);
		this.setLayoutParams(new LayoutParams(450, 74));
		super.setScrollbarFadingEnabled(true);
		super.setSmoothScrollingEnabled(true);
		super.setHorizontalScrollBarEnabled(false);
	}
	
	public UiPagingScrollView(android.content.Context context, android.util.AttributeSet attrs){
		super(context,attrs);
		
		init(context, attrs);
	}
	
	public UiPagingScrollView(android.content.Context context, android.util.AttributeSet attrs, int defStyle){
		super(context,attrs,defStyle);
		
		init(context, attrs);
	}
	
	private void init(android.content.Context context, android.util.AttributeSet attrs){
		if(attrs == null)
			return;
		String v = attrs.getAttributeValue(null, "show_current_position_dot");
		if(v == null)
			return;
	}
	
	@Override
	public void addView(View child) {
		mLayout = (LinearLayout) child;
		super.addView(child);
	}
	
	@Override
	public void addView(View child, int index,
			android.view.ViewGroup.LayoutParams params) {
		mLayout = (LinearLayout) child;
		super.addView(child, index, params);
	}
	
	@Override
	public void addView(View child, int width, int height) {
		mLayout = (LinearLayout) child;
		super.addView(child, width, height);
	}
	
	@Override
	public void addView(View child, android.view.ViewGroup.LayoutParams params) {
		mLayout = (LinearLayout) child;
		super.addView(child, params);
	}

	@Override
	public void addView(View child, int position){
		mLayout = (LinearLayout) child;
		super.addView(child, position);
	}
	
	float mLastTouchX;
	int mPosX;
	float mFirstTouchX;

	private int mLastScrollX;
	private int mLastPageIndex;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		float getX = event.getX();

		switch (action) {
		case MotionEvent.ACTION_UP: {

			if (mFirstTouchX - getX > 10) {
				toRight();
			} else if (mFirstTouchX - getX < -10) {
				toLeft();
			} else {
				toTo(mLastScrollX);
			}
			mLastTouchX = -1;
			mFirstTouchX = -1;
			break;
		}
		case MotionEvent.ACTION_DOWN: {
			final float x = event.getX();
			mLastTouchX = x;
			mFirstTouchX = x;
			break;
		}
		case MotionEvent.ACTION_MOVE: {
			final float x = event.getX();
			if (mLastTouchX == -1)
				mLastTouchX = x;

			if (mFirstTouchX == -1)
				mFirstTouchX = x;

			// Calculate the distance moved
			final float dx = mLastTouchX - x;
			int xx = (int) dx + this.getScrollX();
			if (xx > 0) {
				this.scrollTo((int) dx + this.getScrollX(), 0);
				// this.getScrollX()));
			}

			// Remember this touch position for the next move event
			mLastTouchX = x;

			// Invalidate to request a redraw
			invalidate();
			break;
		}
		}

		return true;
	}
	
	 
	public final void setPageScrollCompletedListener(PageScrollCompletedListener listener){
		mPageScrollCompletedListener = listener;
	}

	public void onPageScrollCompleted(int index, int totalCount) {
		
	}

	public void setScrollPage() {

//		Log.i("log scroll", "HorizontalScrollViewHelper ");

		int x = this.getScrollX();
		int right = getRightScrollX();
		int left = getLeftScrollX();

//		Log.i("log scroll", "x=" + x);
//		Log.i("log scroll", "right=" + right);
//		Log.i("log scroll", "left=" + left);

		if ((right - x) > (x - left)) {
//			Log.i("log scroll", "toLeft");
			toLeft();
		} else {
//			Log.i("log scroll", "toRight");
			toRight();
		}
	}

	public void toRight() {
		toTo(getRightScrollX());
	}

	public void toLeft() {
		toTo(getLeftScrollX());
	}

	private void toTo(final int nextX) {
		if (mIsRunning)
			return;
		if(UiPagingScrollView.this.getScrollX() == nextX)
			return;
		
		Thread threadLeft = new Thread() {
			@Override
			public void run() {
				
				try {
					mIsRunning = true;
					
					while (true) {
						int x = UiPagingScrollView.this.getScrollX();
						int preF = nextX - x;
						int dep = (int) ((((float) x - (float) nextX) / 2) * 1.3);
						if (Math.abs(preF) < 10 || Math.abs(dep) < 10) {
							UiPagingScrollView.this.post(new Runnable() {
								@Override
								public void run() {
									UiPagingScrollView.this.scrollTo(nextX, 0);
								}
							});
							break;
						}

						final int fx = x - dep;
						UiPagingScrollView.this.post(new Runnable() {
							@Override
							public void run() {
								UiPagingScrollView.this.scrollTo(fx, 0);
							}
						});

						try {
							Thread.sleep(30);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				} finally {
					mIsRunning = false;

					onPageScrollCompleted(mLastPageIndex, getTotalPageCount());
					if(mPageScrollCompletedListener != null)
						mPageScrollCompletedListener.PageScrollCompleted(mLastPageIndex, getTotalPageCount());
				}
			}
		};
		threadLeft.start();
	}

	private int getLeftScrollX() {
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		View v = li.getChildAt(0);
		int x = this.getScrollX();
		int w = v.getWidth();
		int leftIndex = x / w;
		if (x % w == 0 && leftIndex > 0) {
			leftIndex--;
		}
		
		int leftX = li.getChildAt(leftIndex).getLeft();
		mLastScrollX = leftX;
		mLastPageIndex = leftIndex;
		return leftX;
	}
	
	private int getRightScrollX() {
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		View v = li.getChildAt(0);

		int thisScrollX =this.getScrollX();
		int vgetWidth = v.getWidth();
		
		int rightIndex =  thisScrollX/ vgetWidth;

		if (rightIndex < li.getChildCount() - 1) {
			rightIndex++;
		}
		
		int rightX = li.getChildAt(rightIndex).getLeft();
		mLastScrollX = rightX;
		mLastPageIndex = rightIndex;
		return rightX;
	}
	
	private int getIndexX(int index){
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		View v = li.getChildAt(0);
		
		int vgetWidth = v.getWidth();
		
//		if(vgetWidth == 0){
//			Log.i("TEST", "getIndexX content width=" +vgetWidth );
//		}
//		else
//		{
//			Log.i("TEST", "getIndexX content width=" +vgetWidth );
//		}
		
		return  index * vgetWidth;
	}
	
	public void setIndexWithoutEvent(int index){
		int x = getIndexX(index);
		mLastScrollX = x;
		mLastPageIndex = index;
		this.scrollTo(x, 0);
	}
	
	
	public void setIndex(int index){
		int x = getIndexX(index);
		mLastScrollX = x;
		mLastPageIndex = index;
		toTo(x);
	}
	
	public int getCurrentIndex() {
		return mLastPageIndex;
	}
	
	public int getTotalPageCount() {
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		return li.getChildCount();
	}
	
	public int getChildACount(){
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		return li.getChildCount();
	}
	
	public View getChildAtIndex(int index){
		LinearLayout li = (LinearLayout) this.getChildAt(0);
		return li.getChildAt(index);
	}
	
}

